/*
 * Copyright (C) 2023 InConnect. All rights reserved.
 */
package auth

import (
	"context"
	//"dqbasepkg/errors"
	//"dqbasepkg/i18n"
	//"dqbasepkg/log"
	//"dqbasepkg/types/consts"
	"dqauthsvc/src/app"
	"dqauthsvc/src/types/pb/auth"
	"dqbasepkg/utils"
	"google.golang.org/grpc"
)

type Auth struct {
	auth.UnimplementedAuthSvcServer
}

func (s *Auth) CheckAuth(ctx context.Context, req *auth.CheckAuthReq) (*auth.CheckAuthRep, error) {
	rep := &auth.CheckAuthRep{
		MaxUserCount: 10000,
		RemainDays:   1000,
	}
	return rep, nil
}

func (s *Auth) Profile(ctx context.Context, req *auth.ProfileReq) (*auth.ProfileRep, error) {
	rep := &auth.ProfileRep{
		Values: map[string]string{
			"PowerbyName": app.Config.Common.PowerbyName,
			"PowerbyUrl":  app.Config.Common.PowerbyUrl,
			"LobbyMsg":    app.Config.Common.LobbyMsg,
			"AdminMsg":    app.Config.Common.AdminMsg,
		},
	}
	return rep, nil
}

func (s *Auth) RootLogin(ctx context.Context, req *auth.RootLoginReq) (*auth.RootLoginRep, error) {
	rep := &auth.RootLoginRep{}
	if req.Account != app.Config.Common.RootAccount {
		rep.ErrorCode = -1
		rep.ErrorMsg = "invalid account"
	} else if utils.GetFinalPassword(req.Password, 2) != utils.GetFinalPassword(app.Config.Common.RootPassword, 0) {
		rep.ErrorCode = -2
		rep.ErrorMsg = "invalid password"
	}
	return rep, nil
}

func Register(grpcServer *grpc.Server) {
	authService := &Auth{}
	auth.RegisterAuthSvcServer(grpcServer, authService)
}
